$(function () {
    //变量抽取
    var dictionary_dataGrid = $("#dictionary_dataGrid");
    var dictionarysn_dataGrid = $("#dictionarysn_dataGrid");
    var dictionary_dialog = $("#dictionary_dialog");
    var dictionarysn_dialog = $("#dictionarysn_dialog");
    var dictionary_form = $("#dictionary_form");
    var dictionarysn_form = $("#dictionarysn_form");

    //目录
    dictionary_dataGrid.datagrid({
        url: "/dictionary/selectRootDictionary.do",
        fitColumns: true,//列自适应
        fit: true,
        columns: [[
            {field: 'name', title: '字典名称', width: 100},
            {field: 'sn', title: '字典编码', width: 100},
            {field: 'info', title: '字典简介', width: 100},
        ]],
        toolbar: "#dictionary_tb",//工具
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        onClickRow: function (index, row) {
            //当点击了目录才初始化明细
            dictionarysn_dataGrid.datagrid({
                url: "/dictionary/selectDictionaryItemBySn.do?sn=" + row.sn,
                fitColumns: true,//列自适应
                fit: true,
                columns: [[
                    {field: 'name', title: '明细名称', width: 100},
                    {field: 'sn', title: '目录编码', width: 100},
                    {field: 'info', title: '明细简介', width: 100},
                ]],
                toolbar: "#dictionarysn_tb",//工具
                rownumbers: true,//行号
                singleSelect: true//只允许选择一行
            })
        }
    })


    //目录
    dictionary_dialog.dialog({
        width: 250,
        height: 220,
        buttons: "#dictionary_btns",
        closed: true,
        onClose: function () {
            dictionary_form.form("clear")
        },
        resizeable:true
    })
    //明细
    dictionarysn_dialog.dialog({
        width: 250,
        height: 220,
        buttons: "#dictionarysn_btns",
        closed: true,
        onClose: function () {
            dictionarysn_form.form("clear")
        },
        resizeable:true
    })

    //方法抽成对象统一管理
    var methodObj = {//添加按钮
        add: function () {
            dictionary_dialog.dialog("open");
            dictionary_dialog.dialog("setTitle", "添加");

        },
        //编辑按钮
        edit: function () {
            var row = dictionary_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要编辑的数据', 'info');
            } else {
                dictionary_form.form("load", row);
                dictionary_dialog.dialog("setTitle", "编辑");
                dictionary_dialog.dialog("open");
            }
        },
        del: function () {
            var row = dictionary_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("提示", "选择要删除的数据", "info");
            } else {
                $.messager.confirm("提示", "是否要删除该条数据", function (verdict) {
                    if (verdict) {
                        $.get('/dictionary/delete.do', {id: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', '删除成功', 'info', function () {
                                    dictionary_dialog.dialog("close");
                                    dictionary_dataGrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert("提示", "删除失败", 'error');
                            }
                        });
                    }
                })
            }
        },
        reload: function () {
            dictionary_dataGrid.datagrid("load");
        },
        save: function () {
            dictionary_form.form("submit", {
                url: "/dictionary/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            dictionary_dialog.dialog("close");
                            dictionary_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                }
            })
        },
        //取消按钮
        cancel: function () {
            dictionary_dialog.dialog("close")
        },
        //明细
        addsn: function () {
            var row = dictionary_dataGrid.datagrid("getSelected");
            $("#dictionarysn_sn").textbox("setValue", row.sn)
            dictionarysn_dialog.dialog("open");
            dictionarysn_dialog.dialog("setTitle", "添加");
        },
        //编辑按钮
        editsn: function () {
            var row = dictionarysn_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要编辑的数据', 'info');
            } else {
                dictionarysn_form.form("load", row);
                dictionarysn_dialog.dialog("setTitle", "编辑");
                dictionarysn_dialog.dialog("open");
            }
        },
        delsn: function () {
            var row = dictionarysn_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("提示", "选择要删除的数据", "info");
            } else {
                $.messager.confirm("提示", "是否要删除该条数据", function (verdict) {
                    if (verdict) {
                        $.get('/dictionary/deleteSn.do', {id: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', '删除成功', 'info', function () {
                                    dictionarysn_dialog.dialog("close");
                                    dictionarysn_dataGrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert("提示", "删除失败", 'error');
                            }
                        });
                    }
                })
            }
        },
        reloadsn: function () {
            dictionarysn_dataGrid.datagrid("load");
        },
        savesn: function () {
            dictionarysn_form.form("submit", {
                url: "/dictionary/saveOrUpdateSn.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            dictionarysn_dialog.dialog("close");
                            dictionarysn_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                }
            })
        },
        //取消按钮
        cancelsn: function () {
            dictionarysn_dialog.dialog("close")
        }
    }

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

});


